{-include file=$smarty.const.B_DIR|cat:'/src/_header.htm'-}


	<script type="text/javascript" src="./src/filter.js"></script>

<script type="text/javascript">
Ext.onReady(function() {
	Ext.QuickTips.init();
	var stuff_group	= {-$stuff_group-}
	var poultry	= {-$poultry-}

	var _stuff_group	= {};
	for(var i in stuff_group.root){
		_stuff_group[parseInt(stuff_group.root[i].id)]	= stuff_group.root[i];
	}
	var _poultry	= {};
	for(var i in poultry.root){
		_poultry[parseInt(poultry.root[i].id)]	= poultry.root[i];
	}


	var update	= function(){
		if( grid.getBottomToolbar().disabled){
			return false;
		}
		var o = sm.getSelections();
		if(  o.length!== 1 ){						
			var o = {
				title: "注意" ,
				msg: "请选择一条记录!" ,
				buttons: Ext.Msg.OK,
				icon: Ext.MessageBox.ERROR
			};
			Ext.MessageBox.show(o);
			return ;
		}
		grid.getBottomToolbar().disable();
		Ext.Ajax.request({
			url: '?act=load',
			success: function(response, options ){
				try{
					var data	= Ext.decode(response .responseText);
					
					window.setTitle('修改饲料 - ' + data['饲料名']);
					window.show();
					tab.activate(tab.items.items[0]);
					form.getForm().setValues(data);
				}catch(e){
					alert('数据异常' + e.message)
				}
			},
			failure: function(response, options){
				alert('数据异常 response failure')
			},
			params:{
				id: o[0].data.id
			}
		});

	}

	var insert = function(){
		grid.getBottomToolbar().disable();
		window. setTitle('添加饲料');
		window.show();
		tab.activate(tab.items.items[0]);
		form.getForm().reset();
		
	}
	var insertOk = function(){
		if( !form.form.isValid()){
			return false;
		}
		form.form.submit({
			method: 'POST',
			waitMsg: '数据保存中..',
			success: function(form, action) {
				window.hide();
				var scroll	= grid.getView().getScrollState();
				store.reload({callback: function(){
					window.hide();
					grid.getView().restoreScroll(scroll);
					grid.getBottomToolbar().enable();
				}});
			},
			failure: function(form , action){ //()  response.responseText
				alert("递交信息失败，请联系管理员")
			}
		});
	}
	
	var store = new Ext.data.JsonStore({
		url: '?act=list',
		root:'root',
		id:'id',
		totalProperty: 'Total',
		 remoteSort : true,
		fields: [
			{name:'id', type:'int'},
			'饲料名',
			'中国饲料号',
			{name:'原料分类', type:'int'},
			{name:'动物种类', type:'int'},
			//单价,配比上限,配比下限,干物质,库存
			{name:'单价', type:'float'},
			{name:'配比上限', type:'float'},
			{name:'配比下限', type:'float'},
			{name:'干物质', type:'float'},
			{name:'库存', type:'float'},
			'描述',
			'备注'
		]	
	});

	store.on('beforeload', function(){
		viewport.disable();
	});

	store.on('load', function(){
		viewport.enable();
	});
		//viewport.enable();
		//grid.getBottomToolbar().enable();



	var barItems	= [ '-',
			{
				text:'添加',
				iconCls:'icon-add',
				handler:insert
			}, '-',
			{
				text:'编辑',
				iconCls:'icon-edit',
				handler: update
			},  '-', {
				text:'删除',
				iconCls:'icon-delete',
				handler: function(){
					var o = sm.getSelections();
					if( ! o.length ){						
						var o = {
							title: "注意" ,
							msg: "请至少选择一条记录!" ,
							buttons: Ext.Msg.OK,
							icon: Ext.MessageBox.ERROR
						};
						Ext.MessageBox.show(o);
						return ;
					}
					var r = []
					Ext.each(o,function(node){
						r.push(node.data.id);
					});
					Ext.MessageBox.confirm('提示', '您确定要删除第'+ r.join(',') +'条记录吗?', function(ret){
						if( ret !== 'yes' ){
							return false;
						}
						grid.getBottomToolbar().disable();	
						Ext.Ajax.request({
							url: '?act=del',
							success: function(response, options ){
								if(response.responseText !='ok'){
									Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！", function(){
										alert(response.responseText);
									})
								}else{
									store.reload({callback: function(){
										grid.getBottomToolbar().enable();
									}});
								}
							},
							failure: function(response, options){
								Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！")
							},
							params: { 
								dels : r.join(',')
							}
						});
					});
					//grid.getBottomToolbar().disable();
				}
			}, '-',  {
				text: '导入',
				iconCls:'icon-hide-inherited',
				handler: function(){
					grid.getBottomToolbar().disable();
					win32.show();
				}
			}
		];

	// ========== 过滤
	
	var filters = new Ext.ux.Filters({
		fields:{
			'原料分类' :_stuff_group,
			'动物种类': _poultry
		}
	});
	
	var sm = new Ext.grid.CheckboxSelectionModel(); 
	var grid = new Ext.grid.GridPanel({
		sm:sm,
		store: store,
		columns: [
			{id:'id',header: "ID", width: 40, sortable: true, dataIndex: 'id'},
			{header: "饲料名", width: 140, sortable: true, dataIndex: '饲料名'},
			{header: "中国饲料号", width: 140, sortable: true,dataIndex: '中国饲料号'},
			{header: "原料分类", width: 80, dataIndex: '原料分类', renderer: function(val){
				return  _stuff_group[val] ? _stuff_group[val]['原料分类'] : '';
			}},
			{header: "动物种类", width: 80, dataIndex: '动物种类', renderer: function(val){
				return  _poultry[val] ? _poultry[val]['动物种类'] : '';
			}},
			{header: "描述", width: 280, sortable: false,dataIndex: '描述'}
		],
		stripeRows: true,
		region:'center',
		plugins: filters,
		bbar: new Ext.PagingToolbar({
			pageSize: 20,
			store: store,
			displayInfo: true,
			displayMsg: '第 {0} - {1} 条  共 {2} 条',
			emptyMsg: "没有数据",
			items:barItems
		})
	});

	grid.on({
		'celldblclick':update
	});
	
	var viewport = new Ext.Viewport({
		layout:'border',
		items:grid
	});
	
	store.load();
	//===================


	var ds1	= new Ext.data.JsonStore({
			url: '{-$upath-}/stuff_group.js',
			root: 'root',
			fields: [
				{name:'id', type:'int'},
				{name: 'txt', mapping:'原料分类'}
			]
		 });
	ds1.load();
	var combo1 = new Ext.form.ComboBox({
		 valueField  :'id',
		displayField:'txt',
		store: ds1,
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		emptyText:'请选择..',
		selectOnFocus:true,
		editable: false, 
		fieldLabel: '原料分类',
		name: '原料分类1',
		hiddenName:'原料分类',
		anchor:'44%'
	});

	var ds2	= new Ext.data.JsonStore({
			url: '{-$upath-}/poultry.js',
			root: 'root',
			fields: [{name:'id', type:'int'},  {name: 'txt', mapping:'动物种类'}]
		 });
	ds2.load();
	var combo2 = new Ext.form.ComboBox({
		valueField  :'id',
		displayField:'txt',
		store: ds2,
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		emptyText:'请选择..',
		selectOnFocus:true,
		editable: false, 
		fieldLabel: '动物种类',
		name: '动物种类1',
		hiddenName:'动物种类',
		anchor:'44%'
	});


	var items2	= [	
			{-foreach from=$units_list item=a key=k-}
			 {
				fieldLabel: '{-$k-}',
				name: 'sArray_{-$k-}'
			 },{-/foreach-}
			{xtype:'hidden'}
			]
	var items1	= [{
				xtype:'hidden',
				name:'id'
			},
			{          
				xtype:'textfield',
				fieldLabel: '饲料名',
				name: '饲料名'
				,allowBlank: false,
				minLength:1
			}, {
				xtype:'textfield',
				reg :/^[\d\-]+$/,
				fieldLabel: '中国饲料号',
				name: '中国饲料号'
			},combo1, combo2,
			
			{
				xtype:'textfield',
				regex: regPercent,
				fieldLabel: '配比上限',
				name: '配比上限'
			},			
			{
				xtype:'textfield',
				regex: regPercent,
				fieldLabel: '配比下限',
				name: '配比下限'
			},			{
				xtype:'textfield',
				regex: regMoney,
				fieldLabel: '单价',
				name: '单价'
			},			
			{
				xtype:'textfield',
				regex: regPercent,
				fieldLabel: '库存',
				name: '库存'
			},

			{
				fieldLabel: '描述',
				name: '描述',
				anchor: '96%'
			},{
				fieldLabel: '备注',
				name: '备注',
				anchor: '96%'
			} ];
	var btns	= [
			{
				text: '保存',
				handler: insertOk
			},{
				text: '取消',
				handler: function(){
					window.hide();
					grid.getBottomToolbar().enable();
				}
			}
		];




try{

	var tab	= new Ext.TabPanel({
			//xtype:'tabpanel',  
			//height: 316,     
			activeTab: 0,
			//layout: 'fit',
			//layoutOnTabChange:true,
			 deferredRender : false,
			 bodyStyle :'padding:4px;height:100%;',
			 style:'height:100%;',
			//bbar:[{text:'a'}],
			items:[ {
					title:'基本属性',
					layout:'form',
					autoScroll : true,
					defaultType: 'textfield',
					items: items1
				},{
					title:'成分',
					layout:'form',
					defaultType: 'textfield',
					autoScroll : true,
					items: items2
				}
			]
		});
	var form = new Ext.form.FormPanel({
		//xtype:'form',
		baseCls: 'x-plain',
		labelWidth: 90,
		url:'?act=save',
		defaultType: 'textfield',
		layout: 'fit', 
		//layout:'column',
		//defaults:{ border:false, bodyStyle:'padding:0px', columnWidth:1}, //columnWidth:0.5,
		items: tab
	});

}catch(e){alert(e.message)}
	//var mask	= new Ext.LoadMask( grid.getEl(),{msg:'数据读取中...',removeMask:true});

	var window = new Ext.Window({
		
		title: '添加原料',
		width: 520,
		height:368,
		minWidth: 400,
		minHeight: 320,
		//plain:true,
		layout: 'fit',
		//bodyStyle:'padding:5px;',
		//maskDisabled : false,
		buttons :btns,
		items: form
	});
	window.on({
		 beforeclose  : function(){
			window.hide();
			grid.getBottomToolbar().enable();
			return false;
		}
	});

	if( Ext. isIE ){
		window.on({
			 resize  : function(){
				tab.setHeight(window.getInnerHeight());
			}
		});		
	}

try{
	var upload2 = new Ext.FormPanel({
		title:'基本属性',
		//xtype:'form',
		fileUpload : true,
		method: 'POST',
		url:'?act=upload' ,
		defaultType: 'textfield',
		items:[
			{
				fieldLabel: '选择文件',
				name: 'xsl',
				width:180,
				inputType: 'file',
				regex : /\.xls$/,
				regexText:'必须选择xls格式文件!',
				allowBlank:false
			}						
		]
	});
	
	var tab2	= new Ext.TabPanel({
			activeTab: 0,
			//layout: 'fit',
			//layoutOnTabChange:true,
			 deferredRender : false,
			 bodyStyle :'padding:4px;height:100%;',
			style:'height:100%;',
			items:[ 
				upload2
			],
			bbar:[ '->',
				{
					text:'发送',
					handler: function(){
						if ( upload2.getForm().isValid() ) {
							upload2.getForm().submit();
						}
					}
				},{
					text: '取消',
					handler: function(){
						win32.hide();
						grid.getBottomToolbar().enable();
					}
				}
			]
		});

	var win32 = new Ext.Window({		
		title: '导入数据',
		width: 320,
		height:160,
		minWidth: 200,
		minHeight: 100,
		//plain:true,
		layout: 'fit',
		//buttons :btns,
		items: tab2
	});

	win32.on({
		 beforeclose  : function(){
			win32.hide();
			grid.getBottomToolbar().enable();
			return false;
		}
	});

	upload2.getForm().on({
		actioncomplete: function(form, action){
			win32.hide();
			upload2.getForm().reset();
			Ext.MessageBox.alert('提示信息', '导入数据成功', function(){		
				viewport.disable();
				store.reload();
			}); 
		},
		actionfailed: function(form, action){
			Ext.MessageBox.alert('提示信息', action. result.msg || '导入数据失败');
		}
	});



}catch(e){
	alert(e.message)
}

});
</script>


{-include file=$smarty.const.B_DIR|cat:'/src/_footer.htm'-}